package com.qiyi.player.core.video.decoder
{
   import com.qiyi.player.core.player.coreplayer.ICorePlayer;
   import com.qiyi.player.core.video.file.actors.datatile.MediaData;
   import flash.utils.ByteArray;
   import com.qiyi.player.base.pub.EnumItem;
   import com.qiyi.player.core.video.def.DecoderStatusEnum;
   import flash.events.NetStatusEvent;
   import flash.net.NetConnection;
   
   public class AdDecoder extends Decoder
   {
      
      public function AdDecoder(param1:ICorePlayer = null) {
         this._holder = param1;
         var _loc2_:NetConnection = new NetConnection();
         _loc2_.connect(null);
         super(_loc2_);
      }
      
      private static const RESET_BEGIN:String = "resetBegin";
      
      private static const RESET_SEEK:String = "resetSeek";
      
      private static const END_SEQUENCE:String = "endSequence";
      
      private var _startTime:int = 0;
      
      private var _allowGetTime:Boolean = true;
      
      private var _endOfFile:Boolean = false;
      
      private var _holder:ICorePlayer;
      
      override public function get time() : Number {
         if(this._allowGetTime)
         {
            return this._startTime + super.time * 1000;
         }
         return this._startTime;
      }
      
      override public function play(... rest) : void {
         super.play(null);
         this._allowGetTime = true;
      }
      
      override public function seek(param1:Number) : void {
         if(param1 < 100 || !(Math.ceil(param1) == param1))
         {
            return;
         }
         this._startTime = param1;
         this._endOfFile = false;
         this._allowGetTime = false;
         _log.info("ad decoder seek:" + param1);
         super.seek(param1);
      }
      
      public function endSequence() : void {
         if(this._endOfFile)
         {
            return;
         }
         this._endOfFile = true;
         this.tryAppendBytesAction(END_SEQUENCE);
         _log.debug("ad appendByteAction:" + END_SEQUENCE);
      }
      
      public function appendData(param1:MediaData) : void {
         if(param1 == null)
         {
            return;
         }
         this._endOfFile = false;
         if(param1.headers)
         {
            this.tryAppendBytesAction(RESET_BEGIN);
            _log.debug("ad appendByteAction:" + RESET_BEGIN);
            this.tryAppendBytes(param1.headers);
            _log.debug("ad append headers");
            this.tryAppendBytesAction(RESET_SEEK);
            _log.debug("ad appendByteAction:" + RESET_SEEK);
         }
         if((param1.bytes) && (param1.bytes.length))
         {
            this.tryAppendBytes(param1.bytes);
            _log.debug("ad append bytes: " + param1.bytes.length);
         }
      }
      
      private function tryAppendBytesAction(param1:String) : void {
         var action:String = param1;
         if(this.hasOwnProperty("appendBytesAction"))
         {
            try
            {
               this["appendBytesAction"](action);
            }
            catch(e:Error)
            {
               _log.warn("ad appendBytesAction Error: " + action);
            }
         }
      }
      
      private function tryAppendBytes(param1:ByteArray) : void {
         if(this.hasOwnProperty("appendBytes"))
         {
            this["appendBytes"](param1);
         }
      }
      
      override protected function setStatus(param1:EnumItem) : void {
         if(param1 == DecoderStatusEnum.PLAYING)
         {
            this._allowGetTime = true;
         }
         super.setStatus(param1);
      }
      
      override public function stop() : void {
         this._allowGetTime = true;
         super.stop();
      }
      
      override protected function onNetStatus(param1:NetStatusEvent) : void {
         _log.debug("ad netstream code:" + param1.info.code);
         switch(param1.info.code)
         {
            case "NetStream.Buffer.Empty":
               break;
            case "NetStream.Seek.Notify":
               this.tryAppendBytesAction(RESET_SEEK);
               break;
            case "NetStream.Buffer.Full":
               this._allowGetTime = true;
               break;
         }
         super.onNetStatus(param1);
      }
   }
}
